$(function(){
  //选项卡
  var tabFn = (function(mod){
    function tabPublicFn(attr1,target,addAttr,attr2,eve){
      var tabTit=$(attr1);
      tabTit.on(eve,target,function(){
        var num = $(this).index();
        $(this).addClass(addAttr).siblings().removeClass(addAttr);
        $(this).parent().siblings().find(attr2).eq(num).show();
        $(this).parent().siblings().find(attr2).eq(num).siblings().hide();
      })
    }
    tabPublicFn('.bannerN_conBox','.bannerN_tit span','bannerN_titCur','.bannerN_conlist','click');
    return mod;
  })(window.tabFn || {});

  //搜索获取焦点样式
  var navSearchFn = (function(mod){
    $('.nav_search .inp_srh').focus(function(){
      $(this).parents('.nav_search').addClass('nav_search_current');
    });
    $('.nav_search .inp_srh').blur(function(){
      $(this).parents('.nav_search').removeClass('nav_search_current');
    });
    return mod;
  })(window.navSearchFn || {});

  //toolbar advert 适配移动端
  if($('#advert').length && $(window).width()<=768){
    $('#advert').css('width','100%');
    if($('.advert-a').length && $('.advert-a').css('background-image')){
      var backimg = $('.advert-a').attr('style');
      backimg = backimg.replace('jpg);','jpg)!important;');
      $('.advert-a').css("cssText",backimg);
    }
  }

  //utm_source渠道标记跟踪
  var enableUtmTrack = true;
  var utm = getUrlUtm(window.location.href);
  var url_include = '.csdn.net';
  // if (enableUtmTrack && (utm == 'utm_source=juhe' || utm == 'utm_source=xueyuanjuhe')) {
  if (enableUtmTrack && utm.length>0) {
    $('body').on('click', 'a:not([id="openWin"])', function(e){
      var url = $(this).attr('href');
      var target = $(this).attr('target');
      //仅对edu.csdn.net链接处理
      if (url.indexOf(url_include) > 0) {
        e.preventDefault();
        if (url.indexOf('?')< 0) {
          url = url + '?' + utm
        } else {
          var _utm = getUrlUtm(url);
          if(_utm.length>0){
            url = url.replace(_utm, utm);
          } else {
            url = url + '&' + utm
          }
        }
        target = target ? target : '_self';
        openWin(url,target);
      }
    })
  }
  function getUrlUtm(url) {
    var utm = '';
    if (url.indexOf('?') < 0) {
      return utm;
    }
    var params = url.split('?')[1].split('&');
    params.forEach(function(item, index){
      var param = item.split('=');
      if(param[0] == 'utm_source') {
        utm = item;
      }
    })
    return utm;
  }
  function openWin(url, target) {
      $('body').append($('<a href="'+url+'" target="'+target+'" id="openWin"></a>'))
      document.getElementById("openWin").click();
      $('#openWin').remove();
  }
});